package juc.juccore.future;

import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.*;

public class MultiFutures {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        ArrayList<Future> futures = new ArrayList<>();
        Callable<Integer> callable = () -> {
            TimeUnit.SECONDS.sleep(2);
            return new Random().nextInt(100);
        };
        for (int i = 0; i < 20; i++) {
            Future<Integer> future = executorService.submit(callable);
            futures.add(future);
        }
        for (int i = 0; i < 20; i++) {
            Future<Integer> future = futures.get(i);
            try {
                Integer integer = future.get();
                System.out.println(integer);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

        }
    }
}
